package com.ktkj.init.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "com.ktkj.init.mapper")
public class MybatisPlusConfiguration {

    /**
     * 分页插件
     * paginationInterceptor.setLimit(你的最大单页限制数量，默认 500 条，小于 0 如 -1 不受限制);
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 实现乐观锁
     * 在实体类Integer字段上加@Version 即可
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

   /*
    *
    *
    实现乐观锁-3.4.0
    @Bean
    public MybatisPlusInterceptor optimisticLockerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }*/

    /*
     * 悲观锁（核心是 for update）
     * select * from goods where id = 1 for update;
     * update goods set stock = stock - 1 where id = 1;
     */

    /*
     * 乐观锁 适用于读多写少的应用场景
     * 悲观锁 适用于读少写多的应用场景
     */


}
